package com.online.service_edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.common_utils.ResultJson;
import com.online.service_edu.entity.EduTeacher;
import com.online.service_edu.entity.vo.TeacherQuery;
import com.online.service_edu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author hdc2008
 * @since 2021-02-05
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/service_edu/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("findAll")
    @ApiOperation(value = "所有讲师列表")
    public ResultJson findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return ResultJson.ok().data("teachers",list);

    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "根据id逻辑删除讲师")
    public ResultJson deleteTeacherById(@ApiParam(name = "id", value = "讲师ID", required = true)@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        return ResultJson.ok();
    }

    @GetMapping("pageTeacher/{current}/{size}")
    public  ResultJson pageTeachers(@PathVariable long current,@PathVariable long size){
        Page<EduTeacher> teacherPage=new Page<>(current,size);
        eduTeacherService.page(teacherPage, null);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return ResultJson.ok().data("total",total).data("rows",records);
    }
    /*
        根据多个条件分页查询
     */
    @PostMapping ("pageTeacherCondition/{current}/{size}")
    public  ResultJson pageTeacherCondition(@PathVariable long current, @PathVariable long size,
                                            @RequestBody(required=false) TeacherQuery teacherQuery){
        Page<EduTeacher> teacherPage=new Page<>(current,size);
        QueryWrapper<EduTeacher> wrapper= new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(level!=null){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(teacherPage, wrapper);
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return ResultJson.ok().data("total",total).data("rows",records);
    }

    /*
    添加讲师的方法
     */
    @PostMapping("addTeacher")
    public ResultJson addTeacher(@RequestBody EduTeacher teacher){
        System.out.println(teacher);
        boolean b = eduTeacherService.save(teacher);
        if(b){
            return ResultJson.ok();
        }else{
            return ResultJson.error();
        }
    }
    /*
    根据讲师id查询
 */

    @GetMapping("findThById/{id}")
    public ResultJson findTeacherById(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        if(teacher!=null){
            return ResultJson.ok().data("teacher",teacher);
        }else{
            return ResultJson.error();
        }
    }

    @PostMapping("updateTh")
    public ResultJson updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return ResultJson.ok();
        }else{
            return ResultJson.error();
        }
    }

}

